package com.api.shopapi.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.api.shopapi.entity.EHealthRecord;
import com.api.shopapi.entity.Elderly;
import com.api.shopapi.entity.User;
import com.api.shopapi.entity.dto.ElderlyDTO;
import com.api.shopapi.entity.dto.ElderlyDTO1;
import com.api.shopapi.entity.dto.ElderlyDTO2;
import com.api.shopapi.mapper.EHealthRecordMapper;
import com.api.shopapi.mapper.ElderlyMapper;
import com.api.shopapi.service.ElderlyService;
import com.api.shopapi.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("ElderlyServiceImplOne")
public class ElderlyServiceImpl implements ElderlyService {

    @Autowired
    private ElderlyMapper elderlyMapper;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;



    @Override
    public int insert(Elderly elderly) {
        return elderlyMapper.insert(elderly);
    }

    @Override
    public
    int delete(String elderlyCode){
        return elderlyMapper.delete(elderlyCode);
    }

    @Override
    public int update(Elderly elderly) {
        return elderlyMapper.update(elderly);
    }

    @Override
    public ElderlyDTO getByElderlyCode(String elderlyCode) {
        return elderlyMapper.getByElderlyCode(elderlyCode);
    }

    @Override
    public ElderlyDTO getByUserCode(String userCode) {
        return elderlyMapper.getByUserCode(userCode);
    }

    @Override
    public PageInfo<ElderlyDTO> getListByGovernmentCode(int pageIndex, int pageSize, String governmentCode,String name,String phone,int status) {
        PageHelper.startPage(pageIndex,pageSize);
        List<ElderlyDTO> elderlyDTOList=elderlyMapper.getListByGovernmentCode(governmentCode,name,phone,status);
        return new PageInfo<>(elderlyDTOList);
    }


    @Override
    public ElderlyDTO2 getElderlyList(String userCode) {
        return elderlyMapper.getElderlyList(userCode);
    }

    @Override
    public PageInfo<ElderlyDTO> getNewElderly(int pageIndex, int pageSize, String name, String idCard, String phone,
                                              int elderlyClass, String zoneCode, String governmentCode,Date startTime,Date endTime) {
        PageHelper.startPage(pageIndex,pageSize);
        List<ElderlyDTO> elderlyDTOList = elderlyMapper.getNewElderly(name,idCard,phone,elderlyClass,zoneCode,governmentCode,startTime,endTime);
        return new PageInfo<>(elderlyDTOList);
    }

    @Override
    public List<ElderlyDTO> getByElderly(String name, String idCard, String phone, int elderlyClass, String zoneCode, String governmentCode, Date startTime, Date endTime) {
        return elderlyMapper.getNewElderly(name,idCard,phone,elderlyClass,zoneCode,governmentCode,startTime,endTime);
    }

    @Override
    public ElderlyDTO getByPhone(String phone) {
        return elderlyMapper.getByPhone(phone);
    }

    @Override
    public ElderlyDTO getByIdCard(String idCard) {
        return elderlyMapper.getByIdCard(idCard);
    }


    //政府查看服务对象列表 模糊查询
    @Override
    public PageInfo<ElderlyDTO> getSelectForGov(int pageIndex, int pageSize,Date startTime, Date endTime, String zoneCode, String governmentCode, String phone, String idCard,String name,int censusRegister) {
        PageHelper.startPage(pageIndex,pageSize);
        List<ElderlyDTO> elderlyDTOList=elderlyMapper.getSelectForGov(startTime, endTime, zoneCode, governmentCode, phone, idCard,name,censusRegister);
        return new PageInfo<>(elderlyDTOList);
    }


    @Override
    public List<ElderlyDTO> getFiltrateBatchRechargeElderly(String zoneCode, String phone, String governmentCode, String name, int censusRegister,int elderlyClass) {
        return elderlyMapper.getFiltrateBatchRechargeElderly(zoneCode, phone, governmentCode, name, censusRegister,elderlyClass);
    }

    @Override
    public PageInfo<ElderlyDTO> getRechargeElderly(int pageIndex,
                                                   int pageSize,
                                                   String zoneCode,
                                                   String phone,
                                                   String governmentCode,
                                                   String name,
                                                   int censusRegister,
                                                   int elderlyClass) {
        PageHelper.startPage(pageIndex,pageSize);
        List<ElderlyDTO> elderlyDTOList=elderlyMapper.getRechargeElderly(zoneCode, phone,governmentCode,name,
                censusRegister,elderlyClass);
        return new PageInfo<>(elderlyDTOList);
    }

    @Override
    public PageInfo<ElderlyDTO> getCarePage(int pageIndex,
                                           int pageSize,
                                           String governmentCode,
                                           String name,
                                           int month,
                                           int day) {
        PageHelper.startPage(pageIndex,pageSize);
        List<ElderlyDTO> elderlyDTOList=elderlyMapper.getCarePage(governmentCode,name,month,day);
        return new PageInfo<>(elderlyDTOList);
    }


    @Override
    public Double getMonetary(String elderlyCode, Date startTime, Date endTime) {
        return elderlyMapper.getMonetary(elderlyCode, startTime, endTime);
    }

    @Override
    public PageInfo<ElderlyDTO> getForConsumerUsers(int pageIndex, int pageSize, String governmentCode, String businessCode, String startTime, String endTime, int orderType, String name) {
        PageHelper.startPage(pageIndex,pageSize);
        List<ElderlyDTO> elderlyDTOList = elderlyMapper.getForConsumerUsers(governmentCode, businessCode, startTime, endTime, orderType, name);
        return new PageInfo<>(elderlyDTOList);
    }

    @Override
    public int getElderlyClassCount(int elderlyClassValue, String governmentCode) {
        return elderlyMapper.getElderlyClassCount(elderlyClassValue,governmentCode);
    }

    @Override
    public int getElderlyClassCountTotal(int elderlyClassValue, String governmentCode) {
        return elderlyMapper.getElderlyClassCountTotal(elderlyClassValue,governmentCode);
    }


    @Override
    public int getElderlyClassCountByNew(int elderlyClassValue, String governmentCode) {
        return elderlyMapper.getElderlyClassCountByNew(elderlyClassValue,governmentCode);
    }

    @Override
    public Elderly getByIcCard(String icCard) {
        return elderlyMapper.getByIcCard(icCard);
    }

    @Override
    public Elderly getByIcCardAllStatus(String icCard) {
        return elderlyMapper.getByIcCardAllStatus(icCard);
    }


    @Override
    public PageInfo<ElderlyDTO> getElderlyDTOByAppNew(int pageIndex, int pageSize, String zoneCode, String governmentCode, String name, String phone) {
        PageHelper.startPage(pageIndex, pageSize);
        List<ElderlyDTO> list = elderlyMapper.getElderlyDTOByAppNew(zoneCode, governmentCode, name, phone);
        return new PageInfo<>(list);
    }



    //查询政府下的老人
    @Override
    public
    List<Elderly> ListElderlyByGovernmentCode(String governmentCode){
        return elderlyMapper.ListElderlyByGovernmentCode(governmentCode);
    }

    //查询政府下到男性老人
    @Override
    public
    List<Elderly> ListElderlyByGovernmentCodeMan(String governmentCode){
        return elderlyMapper.ListElderlyByGovernmentCodeMan(governmentCode);
    }


    @Override
    public String getByElderlyNameToElderlyCode(String elderlyCode) {
        Elderly elderly = elderlyMapper.getByElderlyCode(elderlyCode);
        User user = userService.getByUserCode(elderly.getUserCode());
        return user.getName();
    }


    @Override
    public
    PageInfo<ElderlyDTO> pageByInsCode(int pageIndex, int pageSize, String institutionCode, String name,
                                       String phone,int status, String zoneCode){
        PageHelper.startPage(pageIndex, pageSize);
        List<ElderlyDTO> list = elderlyMapper.listByInsCode(institutionCode,name,phone,status,zoneCode);
        return new PageInfo<>(list);
    }
    @Override
    public
    List<Elderly> listByInsCode(String institutionCode){
        return elderlyMapper.listByInsCode2(institutionCode);
    }

    @Override
    public
    PageInfo<Elderly> pageByInsCodeCheckinStatus(int pageIndex, int pageSize,String institutionCode, String name,
                                                 String phone, int checkinStatus){
        PageHelper.startPage(pageIndex, pageSize);
        List<Elderly> list = elderlyMapper.listByInsCodeCheckinStatus(institutionCode,name,phone,checkinStatus);
        return new PageInfo<>(list);
    }


    @Override
    public
    PageInfo<ElderlyDTO> pageByGovCode(int pageIndex, int pageSize, String governmentCode, String name,
                                       String phone,int status, String zoneCode){
        PageHelper.startPage(pageIndex, pageSize);
        List<ElderlyDTO> list = elderlyMapper.listByGovCode(governmentCode,name,phone,status,zoneCode);
        return new PageInfo<>(list);
    }

    @Override
    public
    PageInfo<Elderly> pageByGovCodeInsCodeName(int pageIndex, int pageSize,
                                                  String governmentCode,
                                                  String institutionCode, String name){
        PageHelper.startPage(pageIndex, pageSize);
        List<Elderly> list = elderlyMapper.listByGovCodeInsCodeName(governmentCode,institutionCode,name);
        return new PageInfo<>(list);
    }

    @Override
    public
    PageInfo<Elderly> pageByGovCodeCheckinStatus(int pageIndex, int pageSize,String governmentCode,
                                                 String name,
                                                 String phone, int checkinStatus){
        PageHelper.startPage(pageIndex, pageSize);
        List<Elderly> list = elderlyMapper.listByGovCodeCheckinStatus(governmentCode,name,phone,checkinStatus);
        return new PageInfo<>(list);
    }



    @Override
    public
    PageInfo<Elderly> pageByInsCodeApplyStatus(int pageIndex, int pageSize,
                                               String institutionCode, String name,
                                               String phone, int applyStatus){
        PageHelper.startPage(pageIndex, pageSize);
        List<Elderly> list = elderlyMapper.listByInsCodeApplyStatus(institutionCode,name,phone,applyStatus);
        return new PageInfo<>(list);
    }

    @Override
    public
    PageInfo<Elderly> pageByGovCodeApplyStatus(int pageIndex, int pageSize,
                                               String governmentCode, String name,
                                               String phone, int applyStatus){
        PageHelper.startPage(pageIndex, pageSize);
        List<Elderly> list = elderlyMapper.listByGovCodeApplyStatus(governmentCode,name,phone,applyStatus);
        return new PageInfo<>(list);
    }


}
